<script lang="jsx" setup>
import dayjs from 'dayjs';
import { computed, nextTick, ref } from 'vue';

const commonSearchRef = ref(null);
const searchParams = ref({
  machineId: null,
  S0001: null,
  S0002: null,
  S0001: null,
  time: null
});
const searchFormList = [
  {
    type: 'input',
    key: 'machineId',
    label: '设备编码',
    placeholder: '请输入设备编码',
    span: 24,
    md: 12,
    lg: 8,
    xl: 8,
    xxl: 4
  },
  {
    type: 'input',
    key: 'S0001',
    label: '生产型号',
    placeholder: '请输入生产型号',
    span: 24,
    md: 12,
    lg: 8,
    xl: 8,
    xxl: 4
  },
  {
    type: 'input',
    key: 'S0002',
    label: '批次号',
    placeholder: '请输入批次号',
    span: 24,
    md: 12,
    lg: 8,
    xl: 8,
    xxl: 4
  },
  {
    type: 'dateRangePicker',
    key: 'time',
    label: '时间',
    span: 24,
    md: 12,
    lg: 10,
    xl: 8,
    xxl: 6,
    showTime: true
  }
];

const tableRef = ref(null);
const column = [
  {
    key: 'dataIndex',
    dataIndex: 'dataIndex',
    title: '序号',
    fixed: 'left',
    width: 70,
    align: 'center',
    checked: true,
    ellipsis: true
  },

  {
    key: 's0001',
    dataIndex: 's0001',
    title: '生产型号',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 's0002',
    dataIndex: 's0002',
    title: '批次号',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 's0003',
    dataIndex: 's0003',
    title: '二维码',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 's0004',
    dataIndex: 's0004',
    title: '测试结果',
    align: 'center',
    checked: true,
    ellipsis: true,
    customRender: ({ record }) => {
      return record.s0004 === 'NG' ? (
        <span class="text-red-500">{record.s0004}</span>
      ) : (
        <span class="text-green-500">{record.s0004}</span>
      );
    }
  },
  {
    key: 's0005',
    dataIndex: 's0005',
    title: '缺陷类型',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 's0006',
    dataIndex: 's0006',
    title: '导通阈值',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 's0007',
    dataIndex: 's0007',
    title: '绝缘阈值',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 's0008',
    dataIndex: 's0008',
    title: '绝缘电压',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'subEqpId',
    dataIndex: 'subEqpId',
    title: '设备编码',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'dateTime',
    dataIndex: 'dateTime',
    title: '检测日期',
    align: 'center',
    checked: true,
    ellipsis: true
  }
];

const searchData = computed(() => {
  const params = {
    ...searchParams.value
  };
  if (searchParams.value?.time?.[0]) {
    params.startTime = dayjs(searchParams.value.time[0]).format('YYYY-MM-DD HH:mm:ss');
    params.endTime = dayjs(searchParams.value.time[1]).format('YYYY-MM-DD HH:mm:ss');
  } else {
    params.startTime = null;
    params.endTime = null;
  }
  delete params.time;
  return params;
});

function search() {
  tableRef.value.getData(1);
}

function reset() {
  nextTick(() => {
    searchParams.value.itemName = null;
    searchParams.value.meshCount = null;
    search();
  });
}
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <CommonSearch
      ref="commonSearchRef"
      v-model:searchParams="searchParams"
      :form-list="searchFormList"
      :is-search="true"
      @search="search"
      @reset="reset"
    ></CommonSearch>
    <Table
      ref="tableRef"
      v-model:searchParams="searchData"
      :column="column"
      :show-row-selection="false"
      row-key="id"
      auto-generate-uuid
      :scroll="{ y: 'auto' }"
      is-init-get-data
      url="/eap/mx320/findAll"
    ></Table>
  </div>
</template>

<style lang="scss" scoped></style>
